﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Entity.vo;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentUpgradeExtra : BasePage
    {
        public Guid RegisterID
        {
            get
            {
                if (hiddenRegisterID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenRegisterID.Value);
            }
            set
            {
                hiddenRegisterID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["RegisterID"] != null) {
                    RegisterID = new Guid(Request.QueryString["RegisterID"]);
                    WsmTaskRegisterEntity entity = new SystemBll().Get<WsmTaskRegisterEntity>(new Guid(Request.QueryString["RegisterID"]));
                    hiddenInstrumentID.Text = entity.InstrumentID.ToString();

                    List<WsmAccessoryInfoEntity> list = new SystemBll().GetAll<WsmAccessoryInfoEntity>(" and AccessoryID in (select AccessoryID from Cms_Wsm_AccessoryUpgradeRegister where RegisterID = '" + RegisterID.ToString() + "')");
                    gridSubItems.DataSource = list;
                    gridSubItems.DataBind();
                    List<WsmAccessoryUpgradeRegisterEntity> upgradeEntitys = new SystemBll().GetAll<WsmAccessoryUpgradeRegisterEntity>(" and RegisterID = '" + RegisterID.ToString() + "' and SoftwareVersion is not null");
                    if (upgradeEntitys != null && upgradeEntitys.Count > 0) {
                        SoftwareVersion.Text = upgradeEntitys[0].SoftwareVersion;
                    }
                }
            }
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (SoftwareVersion.Text.Trim() == "" && gridSubItems.Items.Count == 0) {
                ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('请填写升级内容');", true);
                return;
            }
            SystemBll bll = new SystemBll();
            bll.Delete<WsmAccessoryUpgradeRegisterEntity>(" and RegisterID = '" + RegisterID.ToString() + "'");
            WsmAccessoryUpgradeRegisterEntity entity;
            for (int index = 0; index < gridSubItems.Items.Count; index++) {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                entity = new WsmAccessoryUpgradeRegisterEntity();
                entity.AccessoryID = new Guid(AccessoryID.Text.Trim());
                entity.RegisterID = RegisterID;
                entity.UpGradeID = Guid.NewGuid();
                entity.Deleted = false;
                bll.Insert(entity);
            }
            if (SoftwareVersion.Text.Trim() != "")
            {
                entity = new WsmAccessoryUpgradeRegisterEntity();
                entity.RegisterID = RegisterID;
                entity.UpGradeID = Guid.NewGuid();
                entity.SoftwareVersion = SoftwareVersion.Text.Trim();
                entity.Deleted = false;
                bll.Insert(entity);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('保存成功');", true);
        }

        private DataTable getDateFromGrid(int i)
        {
            DataTable dt = new AccessoryInfo().getAccessoryInfoStructure();
            for (int index = 0; index < gridSubItems.Items.Count; index++)
            {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;         
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;               
                if (index != i)
                {
                    DataRow dr = dt.NewRow();
                    dr["AccessoryID"] = AccessoryID.Text;              
                    dr["AccessoryName"] = AccessoryName.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubItems.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubItems.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = getDateFromGrid(-1);
            bool isExist = false;
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                String AccessoryId = dt.Rows[index]["AccessoryID"].ToString();
                if (AccessoryId == hiddenAccessoryID.Value.Trim())
                {                   
                    isExist = true;
                }
            }
            if (!isExist)
            {
                DataRow dr = dt.NewRow();
                dr["AccessoryID"] = hiddenAccessoryID.Value.ToString();
                WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(hiddenAccessoryID.Value));
                dr["AccessoryName"] = entity.AccessoryName;            
                dt.Rows.Add(dr);
            }
            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();
        }
    }
}
